package com.project.ToDone;

import static org.junit.Assert.*;
import java.util.ArrayList;
import junit.framework.TestCase;
import org.junit.Test;
import android.view.View;

/**
 * JUnit testing for various methods of the ToDone android application
 * 
 * @author Matthew Guzial, Elise Livingston, Sean Wheeler, Patrick Swift
 *
 */

public class ToDoneTest extends TestCase{
        /**
         * 
         * 
         */
        @Test
        public void testRegexNotNull() {
                RegexTester regex = new RegexTester();
                assertTrue(regex.stringNotNullTest("test"));
                assertTrue(regex.stringNotNullTest("tes"));
                assertTrue(regex.stringNotNullTest("te"));
                assertEquals(false, regex.stringNotNullTest(""));
                
        }
        
        /**
         * Test that the regex name check is working properly.
         */
        @Test
        public void testnameTest() {
        	RegexTester regex = new RegexTester();
        	assertTrue(regex.nameTest("John Doe"));
        	assertFalse(regex.nameTest("John"));
        	assertFalse(regex.nameTest("a fh dfk s"));
        	assertFalse(regex.nameTest(""));
        	assertTrue(regex.nameTest("Ivan-Allen Bobsmith"));
        }
        
        /**
         * Checks that the regex time syntax check is working correctly.
         */
        @Test
    	public void testTimeTest() {
        	RegexTester regex = new RegexTester();
        	assertTrue(regex.timeTest("11:50"));
        	assertFalse(regex.timeTest("1:30"));
        	assertFalse(regex.timeTest("abcdefg"));
        	assertFalse(regex.timeTest(""));
        	assertTrue(regex.timeTest("21:40"));
        	assertFalse(regex.timeTest("50:656"));
    	
    }
        /**
         * Checks that the regex date test is working.
         * 
         */
        public void testRegexDate(){
                RegexTester regex = new RegexTester();
                
                //Test that bad calls are bad
                assertFalse(regex.dateTest("99/99/99"));
                assertFalse(regex.dateTest("100/100/100"));
                assertFalse(regex.dateTest("010101"));
                assertFalse(regex.dateTest(""));
                
                //Test that good cals are good
                assertTrue(regex.dateTest("01/01/01"));
                assertTrue(regex.dateTest("12/12/12"));
        }
        
                
                /**
                 * 
                 * JUnit test checks the checkIfValid() method by testing if particular
                 * user names are in the user list and if the method correctly identifies that
                 * information.
                 * 
                 */
              public void testcheckIfValid(){
                        User u1 = new User("Elise", "elivingston@gmail.com", "elivingston3", "fishSticks");
                        User u2 = new User("Paul", "frank@gmail.com", "dicpis4", "password");
                        User u3 = new User("Tamara", "potato@gmail.com", "tamtamb55", "qwerty");
                        User u4 = new User("Jesten", "jmann@gmail.com", "ImAwesome", "hallelujah");
                        ArrayList<User> userList = new ArrayList<User>();
                        userList.add(u1);
                        userList.add(u2);
                        userList.add(u3);
                        userList.add(u4);
                        MasterControlProgram.setUsers(userList);
                        assertFalse("Username that exists is being marked as valid", MasterControlProgram.checkIfValid("elivingston3"));
                        assertTrue("Username that does not exist is being marked as invalid", MasterControlProgram.checkIfValid("IloveCats4440"));
                        ArrayList<User> emptyList = new ArrayList<User>();
                        MasterControlProgram.setUsers(emptyList);
                        assertTrue("Username that does not exist is being marked as invalid", MasterControlProgram.checkIfValid("elivingston3"));
                        emptyList.add(u1);
                        assertFalse("User name that exists is being marked as valid", MasterControlProgram.checkIfValid("elivingston3"));
                        
                        
                }
                
                /**
                 * 
                 * JUnit test for checkIfValidPassword() method. Checks to see whether or
                 * not particular passwords are in the database and whether or not the method
                 * is able to identify that information.
                 * 
                 */
                public void testcheckIfValidPassword(){
                        User u1 = new User("Elise", "elivingston@gmail.com", "elivingston3", "fishSticks");
                        User u2 = new User("Paul", "frank@gmail.com", "dicpis4", "password");
                        User u3 = new User("Tamara", "potato@gmail.com", "tamtamb55", "qwerty");
                        User u4 = new User("Jesten", "jmann@gmail.com", "ImAwesome", "hallelujah");
                        ArrayList<User> userList = new ArrayList<User>();
                        userList.add(u1);
                        userList.add(u2);
                        userList.add(u3);
                        userList.add(u4);
                        MasterControlProgram.setUsers(userList);
                        assertFalse("Existing password does not appear in list", MasterControlProgram.checkIfValidPassword("fishSticks"));
                        assertTrue("Passwords that do not exist are being marked as invalid", MasterControlProgram.checkIfValidPassword("salamander"));
                        ArrayList<User> emptyList = new ArrayList<User>();
                        MasterControlProgram.setUsers(emptyList);
                        assertTrue("Password that does not exist in the list is being marked as invalid", MasterControlProgram.checkIfValidPassword("fishSticks"));
                        emptyList.add(u1);
                        assertFalse("Password in list is being marked as valid.", MasterControlProgram.checkIfValidPassword("fishSticks"));
                        
                        
                }
                
                /**
                 * Test the filter by date function by checking ALL the cases
                 * 
                 * @author Sean Wheeler
                 * 
                 */
                public void testFilterByDateAfter(){
                        ToDoList td = new ToDoList();
                        Event one = new Event(false, false, "04/02/12", "12:00", "", "", "Event 1", "");
                        Event two = new Event(false, false, "04/12/12", "12:00", "", "", "Event 2", "");
                        
                        ArrayList<Event> testList;
                        
                        testList = td.filterEventsByDateAfter("04/01/12");
                        
                        //Assert that no events were returned
                        assertEquals(testList.size(),0);
                        
                        td.addEvent(one);
                        
                        //Check to see that the test arraylist has not changed
                        assertEquals(testList.size(),0);
                        
                        testList = td.filterEventsByDateAfter("04/01/12");
                        
                        //Check to see if the event is returned
                        assertEquals(testList.get(0),one);
                        
                        testList = td.filterEventsByDateAfter("04/03/12");
                        
                        //Check to see if the event is not returned due to not being before the date
                        assertEquals(testList.size(),0);
                        
                        td.addEvent(two);
                        
                        //Check to see the event is not  included in the datalist
                        assertEquals(testList.size(),0);
                        
                        testList = td.filterEventsByDateAfter("04/03/12");
                        
                        //Check to see if the second event is returned
                        assertEquals(testList.get(0),two);
                        
                        testList = td.filterEventsByDateAfter("04/01/12");
                        
                        //Check to see if both events are gotten
                        assertEquals(testList.size(),2);
                        
                        testList = td.filterEventsByDateAfter("04/24/12");
                        
                        //Check to see if both events are not retrieved
                        assertEquals(testList.size(),0);
                        
                        
                        
                }
                
                /**
                 * Test the filter by date function by checking ALL the cases
                 * 
                 * @author Sean Wheeler
                 * 
                 */
                public void testFilterByCompletion(){
                        ToDoList td = new ToDoList();
                        Event one = new Event(false, false, "04/02/12", "12:00", "", "", "Event 1", "");
                        Event two = new Event(false, false, "04/12/12", "12:00", "", "", "Event 2", "");
                        ArrayList<Event> testList;
                        
                        
                        testList = td.filterEventsByCompletion();
                        //Assert that no events were returned
                        assertEquals(testList.size(),0);
                        
                        
                        td.addEvent(one);
                        //Check to see that the test arraylist has not changed
                        assertEquals(testList.size(),0);
                        
                        
                        testList = td.filterEventsByCompletion();
                        //Check to see if the event is returned
                        assertEquals(testList.get(0),one);
                        
                        
                        one.toggleCompletion();
                        testList = td.filterEventsByCompletion();
                        //Check to see if the event is not returned due to not being before the date
                        assertEquals(testList.size(),0);
                        
                        
                        td.addEvent(two);
                        //Check to see the event is not  included in the datalist
                        assertEquals(testList.size(),0);
                        
                        
                        testList = td.filterEventsByCompletion();
                        //Check to see if the first event is the only one
                        assertEquals(testList.get(0),one);
                        
                        
                        two.toggleCompletion();
                        testList = td.filterEventsByCompletion();
                        //Check to see if both events are gotten
                        assertEquals(testList.size(),2);
                        
                        one.toggleCompletion();
                        two.toggleCompletion();
                        testList = td.filterEventsByCompletion();
                        //Check to see if no events are retrieved
                        assertEquals(testList.size(),0);
                        
                        
                        
                }
                
                /**
                 * Test the filter by date before function by checking ALL the cases
                 * 
                 * @author Sean Wheeler
                 * 
                 */
                public void testFilterByDateBefore()
                {
                	 ToDoList td = new ToDoList();
                     Event one = new Event(false, false, "04/03/12", "12:00", "", "", "Event 1", "");
                     Event two = new Event(false, false, "03/01/11", "12:00", "", "", "Event 2", "");
                     
                     ArrayList<Event> testList;
                     
                     testList = td.filterEventsByDateBefore("04/09/12");
                     
                     //Assert that no events were returned
                     assertEquals(testList.size(),0);
                     
                     td.addEvent(one);
                     
                     //Check to see that the test arraylist has not changed
                     assertEquals(testList.size(),0);
                     
                     testList = td.filterEventsByDateBefore("04/09/12");
                     
                     //Check to see if the event is returned
                     assertEquals(testList.get(0),one);
                     
                     testList = td.filterEventsByDateBefore("04/01/12");
                     
                     //Check to see if the event is not returned due to not being before the date
                     assertEquals(testList.size(),0);
                     
                     td.addEvent(two);
                     
                     //Check to see the event is not  included in the datalist
                     assertEquals(testList.size(),0);
                     
                     testList = td.filterEventsByDateBefore("04/01/12");
                     
                     //Check to see if the second event is returned
                     assertEquals(testList.get(0),two);
                     
                     testList = td.filterEventsByDateBefore("04/09/12");
                     
                     //Check to see if both events are gotten
                     assertEquals(testList.size(),2);
                     
                     testList = td.filterEventsByDateBefore("02/01/11");
                     
                     //Check to see if both events are not retrieved
                     assertEquals(testList.size(),0);
                }
                
                
                /**
                 * Test the regex email check function
                 * 
                 * @author Sean
                 */
                public void testRegexEmail()
                {
                	RegexTester rt = new RegexTester();
                	String one = "email@email.com";
                	String two = "@email.com";
                	String three = "email@";
                	String four = "pootis";
                	
                	assertTrue(rt.emailTest(one));
                	
                	assertFalse(rt.emailTest(two));
                	
                	assertFalse(rt.emailTest(three));
                	
                	assertFalse(rt.emailTest(four));
                }
                
                
                
                
                /**
                 * @Author Paco Swift
                 * testValidate tests the user's validate method. This test may
                 * need to be fleshed out more.
                 */
                public void testValidate(){
                        User u1 = new User("Paco", "pswift3@gatech.edu", "ThePaco", "password");
                        User u2 = new User("Elise", "else.livingston@gmail.com", "Moe", "curly");
                        User u3 = new User("Harrison", "hamstereater@hotmail.com", "Papa H", "doodoo");
                        ArrayList<User> userList = new ArrayList<User>();
                        userList.add(u1);
                        userList.add(u2);
                        userList.add(u3);
                        
                        assertEquals(u1.validate("ThePaco","password"),0);
                        assertEquals(u1.validate("ThePaco", "wrongpass"),1);
                        assertEquals(u1.validate("randouser","password"),2);
                        assertEquals(u1.validate("randouse","randopass"),3);
                        assertEquals(u3.validate("Papa H", "doodoo"),0);
                        assertEquals(u3.validate("HARRISONSUCKS","REALLYBAD"),3);
                        assertEquals(u2.validate("Moe", "password"),1);
                        assertEquals(u2.validate("ELISEIMO", "curly"),2);
                }
                /**
                 * @author Paco Swift
                 * tesFilterEventsByType checks the filterEventsByType method to make sure
                 * it returns the correct list of events
                 */
                public void testFilterEventsByType(){
                        
                        ArrayList<Event> results;
                        
                        Event goClass = new Event(true, false, "04/20/12", "12:00", "SCHOOL", "580 Turner Place NW", "Class", "Go learn stuff.");
                        Event goSleep = new Event(true, false, "05/25/12", "12:00", "PERSONAL", "580 Turner Place, NW", "Sleep", "Get some beauty sleep.");
                        Event takeTest = new Event(false, false, "04/03/12", "09:35", "SCHOOL", "580 Turner Place, NW", "Test", "Go get an A on your psych test");
                        ArrayList<Event> badList = new ArrayList<Event>();
                        badList.add(goSleep);
                        badList.add(takeTest);
                        
                        ArrayList<Event> testList = new ArrayList<Event>();
                        testList.add(goClass);
                        testList.add(goSleep);
                        testList.add(takeTest);
                        
                        ArrayList<Event> correct = new ArrayList<Event>();
                        correct.add(goClass);
                        correct.add(takeTest);
                        
                        ToDoList tdl = new ToDoList(testList);
                        results=tdl.filterEventsByType("SCHOOL");
                        assertEquals(results,correct);
                        assertNotSame(results,badList);
                        
                }
                
                /**
                 * Takes an arraylist of users and makes sure that MasterControlPrograms is registering them properly.
                 * 
                 * @author Elise
                 */
                public void testregisterUser(){
                    ArrayList<User> userList = new ArrayList<User>();
                    
                    MasterControlProgram.setUsers(userList);
                    MasterControlProgram.registerUser("Elise", "elivingston@gmail.com", "elivingston", "fishSticks");
                    MasterControlProgram.registerUser("Paul", "frank@gmail.com", "dicpis4", "password");
                    MasterControlProgram.registerUser("Tamara", "potato@gmail.com", "tamtamb55", "qwerty");
                    MasterControlProgram.registerUser("Jesten", "jmann@gmail.com", "ImAwesome", "hallelujah");
                    assertFalse(MasterControlProgram.checkIfValid("elivingston"));
                    assertTrue(MasterControlProgram.checkIfValid("SamIAm"));
                    assertFalse(MasterControlProgram.checkIfValid("dicpis4"));
                    assertFalse(MasterControlProgram.checkIfValid("tamtamb55"));
                    assertTrue(MasterControlProgram.checkIfValid("HotPotato"));
                    assertFalse(MasterControlProgram.checkIfValid("ImAwesome"));
                	
                }
                
                /**
                 * This test takes in an array and makes sure that the login method checks the username and password
                 * are being checked correctly.
                 * 
                 * @author Paco
                 */
                @Test
                public void testLogin(){
                	User u1 = new User("Elise", "elivingston@gmail.com", "elivingston3", "fishSticks");
                    User u2 = new User("Paul", "frank@gmail.com", "dicpis4", "password");
                    User u3 = new User("Tamara", "potato@gmail.com", "tamtamb55", "qwerty");
                    User u4 = new User("Jesten", "jmann@gmail.com", "ImAwesome", "hallelujah");
                    ArrayList<User> userList = new ArrayList<User>();
                    userList.add(u1);
                    userList.add(u2);
                    userList.add(u3);
                    userList.add(u4);
                    MasterControlProgram.setUsers(userList);
                    assertEquals(MasterControlProgram.login("elivingston3", "fishSticks"), 0);
                    assertEquals(MasterControlProgram.login("tamtamb5", "qwerty"), 2);
                    assertEquals(MasterControlProgram.login("ImAwesome", "halleleujah"), 1);
                    assertEquals(MasterControlProgram.login("WrongUser", "password"), 2);
                	
                }
        
}
